<template>
  <div class="app-container calendar-list-container">
    <div class="w-table">
      <buttons :showQuery="true" :showBack="false" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="订单号" :permissions="permissions" @inAdd="doInAdd"
        @outAdd="doOutAdd">
      </buttons>
      <grid gridTitle="加工单" :url="url" :columns="columns" :enableEdit="false" :loading="true" ref="mainGrid" height="auto" diffHeight="90" :pagination="true"
        :permissions="permissions" :multi="false" @change="rowCilck" :queryEntity="queryEntity" @outEdit="doOutEdit" @inEdit="doInEdit" @view="doView" @pass="doPass"
        @cancel="doCancel" @delete="doDelete" @finish="doFinish" @open="doOpen">
      </grid>
    </div>
  </div>
  </div>
</template>
<script>
import request from '@/router/axios'
import { Message, MessageBox } from 'element-ui'
import url from '@/const/apiURL'
import GridDialog from '@/components/GridDialog'
import grid from '@/components/Grid'
import buttons from '@/components/Buttons'
import { mapGetters } from 'vuex'
import { BusinessOrderStatusFormatter, BusinessTypeFormatter, YesOrNoFormatter } from '@/const/dict'
import { bgColor } from '@/const/bgColor'


export default {
  components: { GridDialog, grid, buttons },
  data () {
    return {
      orderNo: '',
      goodsCode: '',
      url: url.NO_ORDER_BUSINESS_LIST_URL,
      entity: {},
      queryEntity: { includeStatus: ["UN_DO", "UN_CHECK", "FINISH"] },
      goods: {},
      disabled: true,
      columns: [
        {
          label: '操作', width: 200, prop: 'op', align: 'center', template: 'op', opList: [
            { name: '编辑', clickName: 'outEdit', auth: 'no_order_out_edit', valueProp: "businessType", showValue: ["OUT"], showFomatter: function (row) { return row.orderStatus == "UN_CHECK" || row.orderStatus == "CANCEL" } },
            { name: '编辑', clickName: 'inEdit', auth: 'no_order_in_edit', valueProp: "businessType", showValue: ["IN"], showFomatter: function (row) { return row.orderStatus == "UN_CHECK" || row.orderStatus == "CANCEL" } },
            { name: '查看', clickName: 'view', auth: 'no_order_view' },
            { name: '取消', clickName: 'cancel', auth: 'business_cancel', confirm: true, valueProp: "orderStatus", showValue: ["UN_DO", "FINISH"] },
            { name: '通过', clickName: 'pass', auth: 'business_pass', confirm: true, valueProp: "orderStatus", showValue: ["UN_CHECK"] },
            { name: '删除', clickName: 'delete', auth: 'business_delete', confirm: true, valueProp: "orderStatus", showValue: ["UN_CHECK"] },
            { name: '完成', clickName: 'finish', auth: 'business_finish', valueProp: "businessType", showValue: ["OUT"], confirm: true, showFomatter: function (row) { return row.status == 0 } },
            { name: '开启', clickName: 'open', auth: 'business_open', valueProp: "businessType", showValue: ["OUT"], confirm: true, showFomatter: function (row) { return row.status == 1 } }
          ]
        },
        { label: '加工单号', width: 180, prop: 'businessCode', align: 'left' },
        { label: '业务类型', width: 100, prop: 'businessType', align: 'center', formatter: BusinessTypeFormatter },
        { label: '客户名称', width: 200, prop: 'customerName', align: 'center' },
        { label: '仓库名称', width: 120, prop: 'warehouseName', align: 'center' },
        { label: '卷数', width: 80, prop: 'total', align: 'center' },
        { label: '数量', width: 90, prop: 'quantity', align: 'center' },
        { label: '订单状态', width: 100, prop: 'orderStatus', align: 'center', formatter: BusinessOrderStatusFormatter, template: 'bgColor', bgColor: bgColor().BusinessOrderStatus },
        { label: '是否完成', width: 80, prop: 'status', align: 'center', formatter: YesOrNoFormatter, template: 'bgColor', bgColor: bgColor().YesOrNo },
        { label: '创建人', width: 120, prop: 'createUserName', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      btnList: [
        { name: '出库', type: 'primary', icon: 'edit', click: 'outAdd', auth: 'no_order_out', ctr: false },
        { name: '入库', type: 'primary', icon: 'edit', click: 'inAdd', auth: 'no_order_in', ctr: false }
      ]
    }
  },
  created () {
    this.orderNo = this.$route.query.orderNo
    this.code = this.$route.query.code
    this.goodsCode = this.$route.query.goodsCode
    this.queryEntity.goodsCode = this.$route.query.goodsCode
    this.queryEntity.orderNo = this.$route.query.orderNo
    this.init()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    init () {
      this.doQuery()
    },
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    doInAdd () {
      let params = {}
      this.$router.push({ path: '/prod/no/order/in', query: params })
    },
    doOutAdd () {
      let params = {}
      this.$router.push({ path: '/prod/no/order/out', query: params })
    },
    doOutEdit (row) {
      if (row) {
        this.$router.push({ path: '/prod/no/order/edit/out', query: { businessCode: row.businessCode } })
      }
    },
    doInEdit (row) {
      if (row) {
        this.$router.push({ path: '/prod/no/order/edit/in', query: { businessCode: row.businessCode } })
      }
    },
    doView (row) {
      if (row) {
        this.$router.push({ path: '/prod/no/order/view', query: { businessCode: row.businessCode } })
      }
    },
    doCancel (row) {
      if (row) {
        this.cancelOp(row.businessCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    cancelOp (businessCode) {
      return request({
        url: url.NO_ORDER_BUSINESS_CANCEL_URL,
        method: 'post',
        data: { key: 'key' },
        params: { businessCode: businessCode }
      })
    },
    doDelete (row) {
      if (row) {
        this.deleteOp(row.businessCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    deleteOp (businessCode) {
      return request({
        url: url.NO_ORDER_BUSINESS_DELETE_URL,
        method: 'post',
        data: { key: 'key' },
        params: { businessCode: businessCode }
      })
    },
    doPass (row) {
      if (row) {
        this.passOp(row.businessCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    passOp (businessCode) {
      return request({
        url: url.NO_ORDER_BUSINESS_PASS_URL,
        method: 'post',
        data: { key: 'key' },
        params: { businessCode: businessCode }
      })
    },
    doFinish (row) {
      if (row) {
        this.finishOp(row.businessCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    finishOp (businessCode) {
      return request({
        url: url.NO_ORDER_BUSINESS_FINISH_URL,
        method: 'post',
        data: { key: 'key' },
        params: { businessCode: businessCode }
      })
    },
    doOpen (row) {
      if (row) {
        this.openOp(row.businessCode).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    openOp (businessCode) {
      return request({
        url: url.NO_ORDER_BUSINESS_OPEN_URL,
        method: 'post',
        data: { key: 'key' },
        params: { businessCode: businessCode }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.w-body {
  padding-top: 8px;
}
.w-table {
  border: 1px solid #ebeef5;
}
.w-table-title {
  border: 1px solid #ebeef5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-break: break-all;
  line-height: 23px;
  display: inline-block;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: relative;
  vertical-align: middle;
  width: 100%;
  font-size: 14px;
  color: gray;
}
</style>
